package logic

import (
	"context"
	"novel/model/mysql"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookshelfListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookshelfListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookshelfListLogic {
	return &BookshelfListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookshelfListLogic) BookshelfList(in *novelrpc.BookshelfListRequest) (*novelrpc.BookshelfListResponse, error) {
	// todo: add your logic here and delete this line

	var user []mysql.BookshelfData

	mysql.DB.Debug().Model(&mysql.Bookshelf{}).
		Select("bookshelves.*, users.id as user_id,novels.id as novel_id").
		Joins("left join users on user_id = bookshelves.user_id").
		Joins("left join novels on novel_id = bookshelves.user_id").
		Find(&user)

	var bookshelf []*novelrpc.BookshelfList
	for _, m := range user {
		list := novelrpc.BookshelfList{
			UserId:  int64(m.UserId),
			NovelId: int64(m.NovelId),
		}
		bookshelf = append(bookshelf, &list)
	}

	return &novelrpc.BookshelfListResponse{
		List: bookshelf,
	}, nil
}
